# class Product:
#     def __init__(self, name, price):  # 实例属性推荐定义在构造方法中
#         self.name = name
#         self.price = price
#
#     def __str__(self):  # 对象描述方法
#         return f"name:{self.name}--- price:{self.price}"
#
#     def add_car(self, car):  # 添加购物车方法  car:购物车的对象
#         car.add(self)
#
# class ShoppingCar:
#     def __init__(self):
#         self.list = []  # 定义商品列表
#
#     def add(self, product):  # 添加商品
#         self.list.append(product)  #product 商品对象
#
#     def show(self):  # 显示所有商品
#         for p in self.list:
#             print(p)
# # 创建商品对象
# product1 = Product("a", 1)
# product2 = Product("b", 2)
# # 创建购物车的对象
# car = ShoppingCar()
#
# car.add(product1)
# product2.add_car(car)
# car.show()   # # 打印购物车中的信息

# # 房子家具管理系统
# class House:
#     def __init__(self, house_type, all_areas):  # 户型，总面积和家具名称列表
#         self.house_type = house_type
#         self.all_areas = all_areas
#         self.useful_area = all_areas  # 剩余可用面积
#         self.furniture_list = []
#
#     def add(self, furniture):  # 添加家具
#         if self.useful_area >= furniture.area:  # 判断家具的面积要小于可用的总面积
#             self.furniture_list.append(furniture)  # 把家具对象添加到家具列表中
#             self.useful_area = self.useful_area - furniture.area  # 更新剩余的总面积
#             print(f"{furniture.name}已经添加，剩余可用面积为{self.useful_area}")
#         else:
#             print("无法添加")
#
#     def show(self):
#         print(f"户型是:{self.house_type}")
#         print(f"总面积：{self.all_areas}")
#         print(f"剩余面积：{self.useful_area}")
#         print("家具列表为")
#         for f in self.furniture_list:
#             print(f)
#
# class Furniture:
#     def __init__(self, name, area):  # 名字和占地面积
#         self.name = name
#         self.area = area
#
#     def __str__(self):
#         return f"Furniture：{self.name},area:{self.area}"
# # 创建房子对象
# my_house = House("三室两厅", 120)

# # 创建家具对象
# bed = Furniture("床", 4)
# wardrobe = Furniture("衣柜", 2)
# table = Furniture("餐桌", 1.5)

# # 把家具添加到房子中
# my_house.add(bed)
# my_house.add(wardrobe)
# my_house.add(table)

# # 打印房子信息
# my_house.show()

# class Counter:
#     count = 0  # 记录类被实例化的次数
#     def __init__(self):
#         # 每次创建一个实例，类属性+1
#         Counter.count +=1
#
#     @classmethod
#     def get_count(cls):
#         return cls.count  # 获取类属性的值
#
#     @classmethod
#     def clear(cls):
#         cls.count = 0  # 给类属性清零
# print(f"类属性的初始值{Counter.count}")
# c1 = Counter()
# c2 = Counter()
# print(f"实例化2次后，类属性的值{Counter.count}")
# print(Counter.get_count())  # 通过类方法获取计数的值
# Counter.clear()  # 计数重置
# print(f"重置后计数值为{Counter.get_count()}")

# class Library:
#     # 定义类属性
#     all_books = 10
#     borrow_books = 0
#
#     def __init__(self, name):
#         self.name = name
#
#     @classmethod
#     def get_books(cls):  # 类方法(获取当前的书籍状态（总书籍数量、已借出书籍数量、可借书籍数量）)
#         useful_books = cls.all_books - cls.borrow_books
#         books_status_info = {"总书籍数量": cls.all_books,
#                              "已借出书籍数量": cls.borrow_books,
#                              "可借书籍数量": useful_books}
#         return books_status_info
#
#     @classmethod
#     def add_books(cls, num):  # 修改总书籍数量（如新增书籍）
#         cls.all_books += num
#         print(f"新增了{num}本书,总书籍数量为:{cls.all_books}")
#
#     def borrow_book(self,num1):
#         if Library.borrow_books + num1 <= Library.all_books:  # 借书<=总书
#             Library.borrow_books += num1
#             print(f"成功借出{num1}本书")
#         else:
#             print("库存不足，借书失败")
#
#     def update_book(self,num2):
#         if num2 <= Library.borrow_books:  # 还书小于借书
#             Library.borrow_books -= num2
#             print(f"成功还{num2}本书")
#         else:
#             print("还书失败")
# # 获取图书馆实例
# my_library = Library("国家图书馆")
# print(f"书籍初始状态：{Library.get_books()}")
# # 新增
# Library.add_books(50)
#
# # 借书
# my_library.borrow_book(20)
# print(f"借书之后状态：{Library.get_books()}")
#
# # 还书
# my_library.update_book(10)
# print(f"还书之后状态：{Library.get_books()}")

# class Calc:
#     @staticmethod
#     def area(long, wide):
#         return long * wide
#
#     @staticmethod
#     def all_long(long, wide):
#         return (long + wide)*2
# # 调用类中的静态方法
# print(f"面积是：{Calc.area(4, 2)}")
# print(f"周长是：{Calc.all_long(4, 2)}")

# class Ecommerce:
#     @staticmethod
#     def calculate_discount(price, discount):  # 原价，折扣
#         if 0 < discount < 100:
#             discount_price = price * (1 - discount/100)
#         else:
#             print(f"传入的折扣{discount}格式错误")
#             discount_price = price
#         return discount_price
#
# goods_list = [{"name": "Apple", "price": 20, "discount": 40},
#               {"name": "Banana", "price": 30, "discount": 60}]
# for g in goods_list:
#     discount_price = Ecommerce.calculate_discount(g["price"], g["discount"])
#     print(f"商品{g['name']}, 原价：{g['price']}, 折扣后价格：{discount_price}")
